/*
 * Copyright (C) 2009 Leandro Aparecido <lehphyro@gmail.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.metalframework.samples.petclinic;

import static com.google.code.liquidform.LiquidForm.*;
import static org.apache.commons.lang.StringUtils.*;
import static org.metalframework.persistence.util.QueryUtils.*;

import java.util.*;

import javax.ws.rs.*;
import javax.ws.rs.core.*;

import org.metalframework.annotation.*;
import org.metalframework.persistence.*;
import org.metalframework.persistence.annotation.*;
import org.metalframework.samples.petclinic.annotation.*;
import org.metalframework.samples.petclinic.exception.*;
import org.springframework.transaction.annotation.*;

import com.google.code.liquidform.*;

@Transactional
@PetClinicComponent
@AutomaticPersistence
@Path("/owners")
public class Clinic {

	@RepositoryQuery
	private Extent<Owner> owners;
	
	@GET @Path("/byLastName/{lastName}") @Produces(MediaType.APPLICATION_JSON)
	@Navigation.Rules({
		@Navigation(view = "/owner.xhtml?id=#{actionResult[0].id}", when = "actionResult.size() == 1", redirect = true),
		@Navigation(view = "/owners.xhtml", when = "actionResult.size() > 1")
	})
	@SuppressWarnings("unchecked")
	public List<Owner> findOwner(@PathParam("lastName") String lastName) throws OwnerNotFoundException {
		Owner owner = alias(Owner.class, "owner");
		SubQuery<Owner> clause = select(owner).from(Owner.class).as(owner);
		
		if (!isBlank(lastName)) {
			clause = ((FromClause<Owner, Owner>)clause).where(like(owner.getLastName(),  forStartsWith(lastName)));
		}
		
		return owners.query(clause);
	}
	
	@GET @Path("/{id}") @Produces(MediaType.APPLICATION_JSON)
	public Owner getOwnerById(@PathParam("id") long id) throws OwnerNotFoundException {
		if (id <= 0) {
			throw new OwnerNotFoundException();
		}
		
		return owners.byId(id, OwnerNotFoundException.class);
	}

	@POST @Consumes(MediaType.APPLICATION_JSON)
	@Navigation(view = "/owner.xhtml?id=#{actionResult}", redirect = true)
	public Long addOwner(@Validate Owner owner) {
		return owners.addInstance(owner);
	}
	
	@PUT @Path("/{id}") @Consumes(MediaType.APPLICATION_JSON)
	@Navigation(view = "/owner.xhtml?id=#{actionParam.id}", redirect = true)
	public void updateOwner(@PathParam("id") Long id, @Validate Owner owner) throws OwnerNotFoundException {
		if (id == null || id <= 0) {
			throw new OwnerNotFoundException();
		}
		
		owners.set(id, owner);
	}
}
